/*This do file is an adaptation of loser.do designed to deal with the particular nature of the Uruguayan master datasets.  This do file can be easily adapted to fit
similar datasets by simply adjusting the top few lines, in which the variables are renamed and the yes, no, etc variables are generated.  Should
variables need to be generated (such as abstain, or missing) or eliminated (such as noresp), references to these will have to be inserted or removed 
in the proper places throughout the file*/

version 7.0
clear
set mem 110m
set maxvar 30000
local y "4"  /* number of highest party*/
local z "1"   /*  number of lowest party*/
local q "22" /*number of votes*/
local country "Uruguay"
local congress "19901994"
local output "c:\output\loser\"
local path "c:\temp\"
local votesfile "C:\WINDOWS\Desktop\Carey\masterdata/`country'master`congress'.dta"


 use "`votesfile'"

rename colyes ayep1
rename colno nayp1

rename nalyes ayep2
rename nalno nayp2

rename fayes ayep3
rename fano nayp3

rename neyes ayep4
rename neno nayp4

egen yes=rsum(ayep*)
egen no=rsum(nayp*)

gen totalvote1=yes+no



forv j=`z'/`y'	{

gen willp`j'=1 if ayep`j'>nayp`j'
replace willp`j'=0 if ayep`j'<=nayp`j' 


			}

gen results=yes/totalvote1
replace results=0 if results==.

gen result=1 if results >.5
replace result=0 if results <=.5


forv j=`z'/`y'		{

gen unityayep`j'=((yes+nayp`j')/(totalvote1))
replace unityayep`j'=0 if unityayep`j'==.
				}				

forv j=`z'/`y'		{
gen unitynayp`j'=(yes-ayep`j')/(totalvote1)

replace unitynayp`j'=0 if unitynayp`j'==.
				}

	/*Now we create the unity could have won scores.*/

forv j=`z'/`y'		{
gen unitychwp`j'=1 if result!= willp`j' & willp`j'==1 & unityayep`j'>.5 
replace unitychwp`j'=1 if result!=willp`j' & willp`j'==0 & unitynayp`j'<=.5
replace unitychwp`j'=0 if unitychwp`j'==.


			}			





/*Now we create the riceloser scores*/			
forv j=`z'/`y'		{

gen riceayep`j'=((yes+nayp`j')/(totalvote1))

replace riceayep`j'=0 if riceayep`j'==.
				}				

forv j=`z'/`y'		{
gen ricenayp`j'=(yes-ayep`j')/(totalvote1)

replace ricenayp`j'=0 if ricenayp`j'==.
				}

forv j=`z'/`y'	{

gen ricechwp`j'=1 if result!=willp`j' & willp`j'==1 & riceayep`j'>.5
replace ricechwp`j'=1 if result!=willp`j' & willp`j'==0 & ricenayp`j'<=.5
replace ricechwp`j'=0 if ricechwp`j'==.
			
				}

forv j=`z'/`y'		{
gen part`j'=1 if (ayep`j'+nayp`j')>=1
			}

forv j=`z'/`y'		{
replace part`j'=0 if part`j'==.
				}

forv j=`z'/`y'	{

count if part`j'==1
gen newpart`j'=r(N)
			}

forv j=`z'/`y'	{
gen tkpart`j'=1 if newpart`j'>=1
			}

forv j=`z'/`y'	{
replace tkpart`j'=0 if newpart`j'<1
			}		


forv j=`z'/`y'	{

count if unitychwp`j'==1
gen uchp`j'= r(N)/`q'
			}	


forv j=`z'/`y'	{

count if ricechwp`j'==1
gen rchwp`j'= r(N)/`q'
			}	

forv j=`z'/`y'	{
count if willp`j'==result 
gen carryp`j'=r(N)

		}


forv j=`z'/`y'	{
replace carryp`j'=0 if tkpart`j'==0
			}

forv j=`z'/`y'		{

gen percarryp`j'=carryp`j'/`q'
			}

drop newpart* part*
/*Now we save this file as penultimate.dta*/
save "`path'\penultimate.dta", replace

drop a* w* result* y* n* ri* uni* tot* c*

save "`path'\chwp.dta", replace
 

keep in 1
drop r* perca* tkpart*
xpose, clear varname
gen str3 party=substr(_varname,5,.)
drop _varname
destring party, replace
sort party
rename v1 unitychwp
save "`path'\unitychwp.dta", replace

use "`path'\chwp.dta", clear
keep in 1
drop u* perca* tkpart*
xpose, clear varname
gen str3 party=substr(_varname,6,.)
drop _varname
destring party, replace
sort party
rename v1 ricechwp
save "`path'\ricechwp.dta", replace

use "`path'\chwp.dta", clear
keep in 1
drop u* r* tkpart*
xpose, clear varname
gen str3 party=substr(_varname,10,.)
drop _varname
destring party, replace
sort party
rename v1 perwon
save "`path'\percarr.dta", replace


use "`path'\chwp.dta", clear
keep in 1
drop u* r* perca*
xpose, clear varname
gen str3 party=substr(_varname,7,.)
drop _varname
destring party, replace
sort party
rename v1 part

save "`path'\tkpart.dta", replace

use "`path'\penultimate.dta", clear
forv j=`z'/`y'		{
gen ustuffed`j'=1 if willp`j'==1 & unitychwp`j'==1
replace ustuffed`j'=0 if ustuffed`j'==.
count if ustuffed`j'==1
gen totustuffed`j'=r(N)
replace totustuffed`j'=0 if totustuffed`j'==.
gen unitystuffed`j'=totustuffed`j'/`q'
}

gen vote=_n
save "`path'\ustuffedvotes.dta", replace

keep unitys*
keep in 1
xpose, clear varname
gen str3 party=substr(_varname,13,.)
drop _varname
destring party, replace
sort party
rename v1 unitystuffed

save "`path'\ustuffed.dta", replace

use "`path'\penultimate.dta", clear
forv j=`z'/`y'		{
gen rstuffed`j'=1 if willp`j'==1 & ricechwp`j'==1
replace rstuffed`j'=0 if rstuffed`j'==.
count if rstuffed`j'==1
gen totrstuffed`j'=r(N)
replace totrstuffed`j'=0 if totrstuffed`j'==.
gen ricestuffed`j'=totrstuffed`j'/`q'
}
gen vote=_n
save "`path'\rstuffedvotes.dta", replace
keep rices*
keep in 1
xpose, clear varname
gen str3 party=substr(_varname,12,.)
drop _varname
destring party, replace
sort party
rename v1 ricestuffed

save "`path'\rstuffed.dta", replace

use "`path'\penultimate.dta", clear

forv j=`z'/`y'		{
gen urolled`j'=1 if willp`j'==0 & unitychwp`j'==1
replace urolled`j'=0 if urolled`j'==.
count if urolled`j'==1
gen toturolled`j'=r(N)
replace toturolled`j'=0 if toturolled`j'==.
gen unityrolled`j'=toturolled`j'/`q'
}

gen vote=_n
save "`path'\urolledvotes.dta", replace
keep unityr*
keep in 1
xpose, clear varname
gen str3 party=substr(_varname,12,.)
drop _varname
destring party, replace
sort party
rename v1 unityrolled

save "`path'\urolled.dta", replace

use "`path'\penultimate.dta", clear


forv j=`z'/`y'		{

gen rrolled`j'=1 if willp`j'==0 & ricechwp`j'==1
replace rrolled`j'=0 if rrolled`j'==.
count if rrolled`j'==1
gen totrrolled`j'=r(N)
replace totrrolled`j'=0 if totrrolled`j'==.
gen ricerolled`j'=totrrolled`j'/`q'
}

gen vote=_n
save "`path'\rrolledvotes.dta", replace
keep ricer*
keep in 1
xpose, clear varname
gen str3 party=substr(_varname,11,.)
drop _varname
destring party, replace
sort party
rename v1 ricerolled

save "`path'\rrolled.dta", replace

use "`path'\unitychwp.dta", clear

merge using "`path'\ricechwp.dta"
drop _merge
merge using "`path'\percarr.dta"
drop _merge
merge using "`path'\rrolled.dta"
drop _merge
merge using "`path'\rstuffed.dta"
drop _merge
merge using "`path'\urolled.dta"
drop _merge
merge using "`path'\ustuffed.dta"
drop _merge

drop if  unitychwp==0 & ricechwp==0 &  (perwon==0 |  perwon==.) &  ricerolled==0 &  ricestuffed==0 &  unityrolled==0 &  unitystuffed==0

rename unitychwp uloser
rename ricechwp rloser
order party perwon uloser unityrolled unitystuffed rloser ricerolled ricestuffed

save "`output'\`country'`congress'loser.dta", replace

use "`path'\ustuffedvotes.dta", clear
keep ustuffed* vote
merge using "`path'\rstuffedvotes.dta"
drop _merge
keep ustuffed* rstuffed* vote
merge using "`path'\urolledvotes.dta"
drop _merge
keep ustuffed* rstuffed* urolled* vote
merge using "`path'\rrolledvotes.dta"
drop _merge
keep ustuffed* rstuffed* urolled* rrolled* vote
merge using "`path'\penultimate.dta"
keep ustuffed* rstuffed* urolled* rrolled* vote unitychwp* ricechwp*
save "`path'\rolledstuffed.dta", replace

forv j=`z'/`y'{
use "`path'\rolledstuffed.dta", replace
keep if unitychwp`j' ==1 | ricechwp`j'==1 | ustuffed`j'==1 | urolled`j'==1 | rstuffed`j'==1 | rrolled`j'==1
gen uloservote_`j'=vote if unitychwp`j'==1
gen rloservote_`j'=vote if ricechwp`j'==1
gen urolledvote_`j'=vote if urolled`j'==1
gen ustuffedvote_`j'=vote if ustuffed`j'==1
gen rrolledvote_`j'=vote if rrolled`j'==1
gen rstuffedvote_`j'=vote if rstuffed`j'==1
drop unitychwp`j' ricechwp`j' ustuffed`j' urolled`j' rstuffed`j' rrolled`j'
keep uloservote* rloservote* ustuffedvote* urolledvote* rstuffedvote* rrolledvote*
if(`j'==`z'){save "`output'\`country'`congress'loservotes.dta", replace}
else {
merge using "`output'\`country'`congress'loservotes.dta"
drop _merge
aorder
save "`output'\`country'`congress'loservotes.dta", replace
}
}
